import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { NO_RESET_WHITE_LIST } from '@/constants'
import { PageFilling } from '@code/components/anvil-code-low-render'
import code from '@/platform/code/router'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: 'index',
    name: 'Root',
    meta: {
      title: '首页'
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/Index/index.vue'),
        name: '首页',
        meta: { title: '首页', icon: 'dashboard', noCache: true, affix: true }
      },
      {
        path: 'Page/PageFilling/:pageId',
        component: PageFilling,
        name: 'PageFilling',
        hidden: true,
        meta: { title: '表单填报' }
      },
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'RouteRedirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    hidden: true,
    meta: {
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    hidden: true,
    meta: {
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/personal',
    component: Layout,
    redirect: '/personal/personal-center',
    name: 'Personal',
    hidden: true,
    meta: {
      title: t('router.personal'),
      canTo: true
    },
    children: [
      {
        path: 'personal-center',
        component: () => import('@/views/Personal/PersonalCenter/PersonalCenter.vue'),
        name: 'PersonalCenter',
        hidden: true,
        meta: {
          title: t('router.personalCenter'),
          canTo: true
        }
      }
    ]
  },
  {
    path: '/resetPassword',
    component: Layout,
    name: 'Reset',
    redirect: '/personal/resetPassword',
    hidden: true,
    meta: {
      title: '重置',
      canTo: true
    },
    children: [
      {
        path: '/personal/resetPassword',
        component: () => import('@/views/Personal/PersonalCenter/resetPassword.vue'),
        name: 'ResetPassword',
        hidden: true,
        meta: {
          title: '重置密码',
          canTo: true
        }
      }
    ]
  },

  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    hidden: true,
    meta: {
      title: '404',
      noTagsView: true
    }
  },
  // 字典详情路由
  {
    path: '/dict',
    component: Layout,
    name: 'Dict',
    hidden: true,
    meta: {},
    children: [
      {
        path: 'type/data/:dictId(\\d+)',
        component: () => import('@base/views/base/dict/data.vue'),
        name: 'BaseDictData',
        meta: { title: '字典数据', icon: '' }
      }
    ]
  },
  {
    path: '/sso',
    component: () => import('@/views/sso.vue'),
    meta: {},
    name: 'sso',
    hidden: true
  },
  {
    path: '/transDesign',
    // component: Layout,
    name: 'transDesign',
    component: () => import('@data/views/data/dataDevelopment/edit.vue'),
    hidden: true,
    meta: {title: '转换流程设计器', noTagsView: true}
  },
  code
]

export const asyncRouterMap: AppRouteRecordRaw[] = []

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !NO_RESET_WHITE_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
